package status

type StatusCode int

const (
	/*
		1: 登陆认证失败
		2：用户请求数据处理失败
		3：服务端处理失败
	*/
	StatusOK           = StatusCode(0)
	StatusInvalidSign  = StatusCode(1100)
	StatusLoginFailed  = StatusCode(1103)
	StatusAccessDenied = StatusCode(2100)
	// 请求参数合法性验证(GET)
	StatusNotLegalInput = StatusCode(2101)
	// 地址错误
	StatusNotFound = StatusCode(2404)
	// json请求校验失败(PUT,POST)
	StatusBodyInvalid          = StatusCode(2104)
	StatusDataProcessingFailed = StatusCode(3100)
	StatusSaveDataFailed       = StatusCode(3101)
)

var statusText = map[int]string{
	0:    "成功",
	1100: "无效的签名",
	1103: "登陆失败",
	2100: "拒绝访问",
	2101: "参数校验失败",
	2104: "请求的数据格式错误",
	3100: "系统处理数据失败",
	3101: "保存数据失败",
}

type BaseMsg struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

type ErrMsg struct {
	BaseMsg
	Detail interface{} `json:"detail"`
}
type RespData struct {
	BaseMsg
	DataList    interface{} `json:"dataList"`
	TotalRecord int         `json:"totalRecord"`
	PageSize    int         `json:"pagesize"`
	PageNo      int         `json:"pageno"`
}

func (sc StatusCode) Msg(msg ...interface{}) *ErrMsg {
	code := int(sc)
	m := &ErrMsg{
		BaseMsg: BaseMsg{
			Code:    code,
			Message: statusText[code],
		},
	}
	if len(msg) > 0 {
		m.Detail = msg[0]
	}
	return m
}

func NewRespData(data interface{}, tr int) *RespData {
	m := &RespData{
		BaseMsg:     BaseMsg{Code: 0, Message: "成功"},
		DataList:    data,
		TotalRecord: tr,
	}
	// m.Code = 0
	// m.Message = "成功"
	return m
}
